﻿package Albums 
{
	import Common.Constants;
	import Common.LabelButton;
	import Common.Room;
	import Core.IconButton;
	import Core.Main;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	import Fonts.CalibriEmbed;
	/**
	 * ...
	 * @author Serg de Adelantado
	 */
	public class AlbumsManager extends Sprite
	{
		private const NAME_LABEL:String = 'Название номера';
		private const DEFAULT_NAME:String = 'Новый номер';
		
		private const ROMMS_LABEL:String = 'Количество номеров';
		private const DEFAULT_ROOMS:String = '0';
		
		private const APARTAMENTS_LABEL:String = 'Количество комнат';
		private const DEFAULT_APARTAMENTS:String = '0';
		
		private const IN_ROOM_LABEL:String = 'В номере';
		private const DEFAULT_IN_ROOM:String = '';
		
		private const ABOUT_LABEL:String = 'Описание номера';
		private const DEFAULT_ABOUT:String = 'Нет описания';
		
		private var FWidth:int;
		private var FHeight:int;		
		private var FContainer:Sprite;		
		private var FFormat:TextFormat;
		
		private var FName:TextField;
		private var FNameLabel:TextField;		
		private var FRooms:TextField;
		private var FRoomsLabel:TextField;		
		private var FApartments:TextField;
		private var FApartmentsLabel:TextField;		
		private var FInRoom:TextField;
		private var FInRoomLabel:TextField;
		private var FAbout:TextField;
		private var FAboutLabel:TextField;
		
		private var FRoot:Main;		
		private var FRoom:Room;		
		private var FRoomIcon:AlbumIcon;		
		private var FAccept:LabelButton;
		private var FCancel:LabelButton;
		
		public function AlbumsManager()
		{
			mouseEnabled = visible = false;			
			
			var vFont:CalibriEmbed = new CalibriEmbed();
			FFormat = new TextFormat();
			FFormat.font = vFont.fontName;
			FFormat.size = 14;			
			
			FContainer = new Sprite();
			addChild(FContainer);
			FContainer.x = 10;
			FContainer.y = 10;
			
			FNameLabel = new TextField();
			ProcessStaticText(FNameLabel, NAME_LABEL);			
			FName = new TextField();						
			ProcessInputText(FName, DEFAULT_NAME);			
			
			FRoomsLabel = new TextField();
			ProcessStaticText(FRoomsLabel, ROMMS_LABEL);			
			FRooms = new TextField();						
			ProcessInputText(FRooms, DEFAULT_ROOMS);					
			FRooms.y = FRoomsLabel.y = FRooms.height + 5;
			
			FApartmentsLabel = new TextField();
			ProcessStaticText(FApartmentsLabel, APARTAMENTS_LABEL);			
			FApartments = new TextField();			
			ProcessInputText(FApartments, DEFAULT_APARTAMENTS);					
			FApartments.y = FApartmentsLabel.y = (FRooms.height + 5) * 2;
			FApartments.restrict = FRooms.restrict = '0-9';
			FApartments.maxChars = FRooms.maxChars = 4;
			
			FInRoomLabel = new TextField();
			ProcessStaticText(FInRoomLabel, IN_ROOM_LABEL);
			FInRoom = new TextField();
			ProcessInputText(FInRoom, DEFAULT_IN_ROOM);
			FInRoom.y = FInRoomLabel.y = (FRooms.height + 5) * 3;
			
			FAboutLabel = new TextField();
			ProcessStaticText(FAboutLabel, ABOUT_LABEL);	
			FAbout = new TextField();		
			ProcessInputText(FAbout, DEFAULT_ABOUT);
			FAbout.y = FAboutLabel.y = (FRooms.height + 5) * 4;
			FAbout.multiline = true;
			FAbout.wordWrap = true;
			
			FInRoom.maxChars = FAbout.maxChars = 1023;
			
			FName.restrict = Constants.INPUT_RESTRICT;
			FAbout.restrict = Constants.INPUT_RESTRICT;
			FInRoom.restrict = Constants.INPUT_RESTRICT;
			
			FName.addEventListener(Event.CHANGE, OnNameInput);
			FRooms.addEventListener(Event.CHANGE, OnRoomsInput);
			FApartments.addEventListener(Event.CHANGE, OnApartmentsInput);
			FInRoom.addEventListener(Event.CHANGE, OnInRoomInput);
			FAbout.addEventListener(Event.CHANGE, OnAboutInput);		
		}		
		
		private function OnNameInput(e:Event):void 
		{			
			FRoomIcon.Name = TextField(e.target).text;
		}
		
		private function OnRoomsInput(e:Event):void 
		{
			FRoomIcon.RoomsCount = TextField(e.target).text;
		}
		
		private function OnApartmentsInput(e:Event):void 
		{
			FRoomIcon.ApartmentsCount = TextField(e.target).text;
		}
		
		private function OnInRoomInput(e:Event):void 
		{
			FRoomIcon.InRoom = TextField(e.target).text;
		}
		
		private function OnAboutInput(e:Event):void 
		{
			FRoomIcon.About = TextField(e.target).text;
		}
		
		private function ProcessInputText(AText:TextField, ALabel:String):void
		{
			AText.background = true;
			AText.type = TextFieldType.INPUT;
			AText.backgroundColor = 0xFFFFFF;
			AText.border = true;
			AText.borderColor = 0x333333;
			AText.defaultTextFormat = FFormat;
			AText.text = ALabel;
			AText.height = AText.textHeight + 4;
			AText.alpha = 0.75;
			AText.addEventListener(KeyboardEvent.KEY_DOWN, OnChange);
			AText.addEventListener(FocusEvent.FOCUS_OUT, OnFocusOut);			
			FContainer.addChild(AText);
		}
		
		private function ProcessStaticText(AText:TextField, ALabel:String):void
		{
			AText.selectable = AText.mouseEnabled = false;			
			AText.defaultTextFormat = FFormat;
			AText.text = ALabel;
			AText.width = AText.textWidth + 5;			
			AText.height = AText.textHeight + 2;
			FContainer.addChild(AText);
		}
		
		public function SetSize(AWidth:int, AHeight:int):void
		{
			FWidth = AWidth;
			//FHeight = AHeight;
			FHeight = 140;
			graphics.clear();
			graphics.beginFill(Constants.ALBUM_BACKGROUND, Constants.ALBUM_ALPHA);
			graphics.drawRect(0, 0, FWidth, FHeight);
			graphics.endFill();
			
			FName.x = FAboutLabel.width + FAboutLabel.x;
			
			FRooms.y = FRoomsLabel.y = FName.y;
			FRooms.width = 50;			
			
			FApartments.y = FApartmentsLabel.y = FName.y;
			FApartments.width = 50;
			
			var vWidth:int = FRoomsLabel.width + FRooms.width + FApartmentsLabel.width + FApartments.width + FNameLabel.width + 30;
			FName.width = FWidth - vWidth;
			FRoomsLabel.x = FName.x + FName.width + 5;
			FRooms.x = FRoomsLabel.width + FRoomsLabel.x;
			FApartmentsLabel.x = FRooms.x + FRooms.width + 5;
			FApartments.x = FApartmentsLabel.width + FApartmentsLabel.x;			
			
			FAbout.x = FInRoom.x = FName.x;
			FAbout.width = FWidth - FAbout.x - 20;
			FAbout.height = FInRoom.height = FName.height * 2;
			FAbout.y = FAboutLabel.y = FInRoom.y + FInRoom.height + 5;
			
			FInRoom.width = FWidth - FInRoom.x - 20;			
			FInRoom.y = FInRoomLabel.y = FName.y + FName.height + 5;			
			/*
			FRooms.x = FRoomsLabel.width + 5;
			FRooms.width = AWidth - FRoomsLabel.width - 30;
			FName.x = FRooms.x;
			FName.width = FRooms.width;
			FApartments.x = FRooms.x;
			FApartments.width = FRooms.width;
			FInRoom.x = FRooms.x;
			FInRoom.width = FRooms.width;
			FAbout.x = FRooms.x;
			FAbout.width = FRooms.width;
			*/
		}
		
		private function OnChange(e:KeyboardEvent):void 
		{
			if (e.keyCode == Keyboard.ENTER)			
				stage.focus = null;			
		}
		
		private function OnFocusOut(e:FocusEvent):void 
		{
			
		}
		
		//{ Getters & setters 
		public function set CurrentRoom(ARoom:AlbumIcon):void 
		{
			stage.focus = null;
			FRoomIcon = ARoom;
			FRoom = ARoom.AlbumRoom;
			FName.text = FRoom.Name;
			FRooms.text = ARoom.RoomsCount;
			FApartments.text = ARoom.ApartmentsCount;
			FInRoom.text = ARoom.InRoom;
			FAbout.text = ARoom.About;
 		}
		
		public function set Root(ARoot:Main):void 
		{
			FRoot = ARoot;
 		}
		//} endregion
		//End of getters & setters
	}
}